package com.cloud.pole.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.user.biz.BaseUserBiz;
import com.cloud.pole.user.model.entity.BaseUser;
import com.cloud.pole.user.service.BaseUserService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/baseUser/admin")
@Slf4j
public class BaseUserController extends BaseController<BaseUserBiz, BaseUser> {

	@Autowired
	private BaseUserService baseUserService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<BaseUser> list(Integer pageNo, Integer pageSize, String queryType, String queryVal) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			Map<String, Object> params = new HashMap<>();
			if (queryType == null || queryType.equals("")) {
			} else {
				if (queryType.equals("1")) {
					params.put("userId", queryVal);
				} else if (queryType.equals("2")) {
					params.put("realName", queryVal);
				} else if (queryType.equals("3")) {
					params.put("mobile", queryVal);
				} else if (queryType.equals("4")) {
					params.put("marketCode", queryVal);
				}
			}
			PageInfo<BaseUser> pageInfo = baseUserService.selectPageShopGoodsAdminList(params, pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public ApiRestResponse<BaseUser> getByUserId(@PathVariable("id") String userId) {
		try {
			BaseUser selectById = baseUserService.selectById(Integer.valueOf(userId));
			return ApiRestResponse.success("200", selectById);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@GetMapping("getByMarketCodeCp")
	@ResponseBody
	public ApiRestResponse<BaseUser> getByMarketCodeAp(String marketCode) {
		try {
			String msg = "";
			BaseUser selectByMarketCode = new BaseUser();
			if (marketCode == null || marketCode.equals("")) {
			} else {
				selectByMarketCode = baseUserService.selectByMarketCode(marketCode);
				if (selectByMarketCode == null) {
					msg = "200";
				} else {
					msg = "300";
				}
			}
			return ApiRestResponse.success(msg, selectByMarketCode);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@PutMapping("updateBaseUser")
	@ResponseBody
	public ApiRestResponse<BaseUser> updateBaseUser(@RequestBody BaseUser baseUser) {
		try {
			baseUserService.updayeByBaseUser(baseUser);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}

	}
}
